package yl.hs.bmipfull.controllers.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IDataDictService;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * DataDictItemController 数据字典数据项接口控制器
 *
 * @author 谢景广
 * @category 系统管理——字典数据项
 */
@Api(value = "字典数据项信息Web接口", tags = "系统管理——字典数据项")
@RequestMapping(path = "/api/dict/item")
@RestController
public class DataDictItemController extends ControllerBase {

    @Autowired
    IDataDictService diSvc;

    @ApiOperation(value = "查询字典数据项", notes = "查询字典数据项")
    @RequestMapping(path = "/type/{typeSn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult queryItemByTypeSN(@PathVariable("typeSn") String typeSn) {
        try {
            var data = diSvc.findDictItemByTypeSN(typeSn);
            if (data != null && data.size() > 0) {
                return success("ok", data);
            } else
                return success("没有找到任何信息");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "保存字典数据项", notes = "保存编辑或者新建的字典数据项")
    @RequestMapping(path = "", method = RequestMethod.POST)
    @ResponseBody
    public WebResult save(@RequestBody SubmitModel model) {
        try {
            var result = diSvc.saveDataItem(model);
            if (result > 0)
                return success("保存成功");
            else
                return fail("保存失败");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询字典数据项", notes = "查询字典数据项编号获取字典数据项")
    @RequestMapping(path = "{sn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult find(@ApiParam(value = "数据编号", required = true) @PathVariable("sn") String sn) {
        try {
            var data = diSvc.findDataItemBySN(sn);
            if (data != null)
                return success("ok", data);
            else
                return notfound(String.format("没有找到编号为%s的数据项", sn));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "删除字典数据项信息", notes = "根据字典数据项编号删除字典数据项信息")
    @RequestMapping(path = "{sn}", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@ApiParam(value = "数据编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn) {
        try {
            var result = diSvc.deleteDataItem(sn.split(","));
            if (result > 0) {
                return success("信息已删除");
            } else
                return fail("没有删除任何信息");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "删除字典数据项信息", notes = "根据字典数据项编号删除字典数据项信息")
    @RequestMapping(path = "/code/{typeCode}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult findDataItemByTypeCode(@ApiParam(value = "数据编号", name = "typeCode", required = true) @PathVariable("typeCode") String typeCode) {
        try {
            var result = diSvc.findDictItemByCode(typeCode);
            if (result != null && result.size() > 0) {
                return success("ok", result);
            } else
                return notfound("没有找到任何信息");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}
